package com.example.segment_saver;
/*
 * Nov 16th 2012
 * Created By: Alex Drotar, Jacob Reiger 
 * Using code from: Ken Anderson 
 * Segment_Saver- This is meant to be run on Andriod mobile devices
 * 					The app will allow the user to take in a url that they
 * 					want to watch. If the user selects the update site button
 * 					The application will go and check if the site has changed 
 * 					since the last time the user looked at it.
 * 
 */
import android.annotation.TargetApi;
import android.app.ListFragment;
import android.content.Intent;
import android.net.Uri;
//import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class DeleteSite extends ListFragment {
	
	
	private SiteList sites = new SiteList();
	private int itemSelected =-1;
	private ArrayList<Integer> itemsSelected = new ArrayList<Integer>();
	private ArrayList<String> sitesSelected	= new ArrayList<String>();
	
	@Override
	public void onResume(){
		super.onResume();
		setListAdapter(getCurrentSites());
		setHasOptionsMenu(true);
		itemsSelected.clear();
		sitesSelected.clear();

	}
	@Override
	public void onPause(){
		super.onPause();
		setListAdapter(getCurrentSites());
		sites.save(new File(getActivity().getFilesDir(), "sites.bin"));
		setHasOptionsMenu(true);
		itemsSelected.clear();
		sitesSelected.clear();

	}

	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
	{
		inflater.inflate(R.menu.fragment_delete_list, menu);
	}
	
	@Override
	public void onPrepareOptionsMenu(Menu menu){
		super.onPrepareOptionsMenu(menu);
		MenuItem delete = menu.findItem(R.id.menu_delete_site);
		//Dont think I will have an edit function but I could add it here
		//MenuItem edit = menu.findItem(R.id.menu_edit_site);
		
		if(itemSelected!=-1)
		{
			delete.setEnabled(true);
			//edit.setEnabled(true);
		}
		else
		{
			delete.setEnabled(false);
			//edit.setEnabled(false);
		}
	}
	@Override
	public boolean onOptionsItemSelected (MenuItem item)
	{
		//Intent intent = new Intent(getActivity(), Delete_Site.class);
		//if(item.getItemId()== R.id.menu_new_site)
		//{
		//	getActivity().startActivity(intent);
		//}
		//Log.i("Segment Saver", "Delete Button Selected Item number: "+ itemSelected);

		if(item.getItemId() == R.id.menu_delete_site)
		{

			//TextView text = (TextView) getListView().getChildAt(itemSelected);
			//remove_site(text.getText().toString());
			
			for(String i: sitesSelected)
			{
				Log.i("SegmentSaver", "Delete Sites" +i);
				remove_site(i);
			}
			itemsSelected.clear();
			sitesSelected.clear();
			//Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(text.getText().toString()));
			//startActivity(browserIntent);
			
		}
		getActivity().recreate();
		//getActivity().finish();
		return true;
	}
	
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id)
	{
		// I think this is where I would have to make it so I could have box's instead of radials but not sure
		registerForContextMenu(v);
		itemSelected=position;
		itemsSelected.add(position);
		TextView text= (TextView) getListView().getChildAt(position);
		sitesSelected.add(text.getText().toString());
		Log.i("SEG SAVER DeleteSite", "Item Selected is :" +itemSelected);
		//getActivity().
		getActivity().invalidateOptionsMenu();
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState){
		super.onActivityCreated(savedInstanceState);
		setEmptyText("No Sites To Remove.");
		setListAdapter(getCurrentSites());
		// TODO:Might have to change the ListView.CHOICE_MODE_SINGLE.
		getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
	}
	
	
	public void remove_site(String site){
		sites.removeSite(site);
		sites.save(new File(getActivity().getFilesDir(), "sites.bin"));
		setListAdapter(getCurrentSites());
		itemSelected=-1;
		getActivity().invalidateOptionsMenu();
	}
	
	public ArrayAdapter<String> getCurrentSites()
	{
		// Made this Multiple choice as well because of the one above. 
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_multiple_choice );
		sites.clear();
		sites.load(new File(getActivity().getFilesDir(), "sites.bin"));
		
		for(String name : sites){
			adapter.add(name);
		}
		
		return adapter;
	}
				
}

